package com.zzh.controller;

import com.zzh.domain.Menu;
import com.zzh.domain.ResponseResult;
import com.zzh.domain.Role;
import com.zzh.domain.RoleMenuVo;
import com.zzh.service.MenuService;
import com.zzh.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色模块
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;
    @Autowired
    private MenuService menuService;

    @RequestMapping("/findAllRole")
    public ResponseResult findAllRole(@RequestBody Role role) {
        List<Role> roles = roleService.findAllRole(role);
        ResponseResult result = new ResponseResult(true, 200, "成功响应", roles);
        return result;
    }

    /**
     * 还没完成，正在开发中
     */
    @RequestMapping("/saveOrUpdateRole")
    public ResponseResult saveOrUpdateRole(Role role) {
        if (role.getId() == null) {
            roleService.saveRole(role);
            return new ResponseResult(true, 200, "成功响应", "");
        } else {
            roleService.updateRole(role);
            return new ResponseResult(true, 200, "成功响应", "");
        }
    }

    @RequestMapping("/findAllMenu")
    public ResponseResult findAllMenu() {
        //-1表示查询所有菜单
        List<Menu> menus = menuService.findSubMenuListByPid(-1);
        Map<String, Object> map = new HashMap<>();
        map.put("parentMenuList", menus);
        return new ResponseResult(true, 200, "成功响应", map);
    }

    @RequestMapping("/findMenuByRoleId")
    public ResponseResult findMenuByRoleId(Integer roleId) {
        List<String> menuString = roleService.findMenuByRoleId(roleId);
        return new ResponseResult(true, 200, "成功响应", menuString);
    }

    @RequestMapping("/RoleContextMenu")
    public ResponseResult RoleContextMenu(@RequestBody RoleMenuVo roleMenuVo) {
        roleService.RoleContextMenu(roleMenuVo);
        return new ResponseResult(true, 200, "成功响应", "");
    }

    @RequestMapping("/deleteRole")
    public ResponseResult deleteRole(int id) {
        roleService.deleteRole(id);
        return new ResponseResult(true, 200, "成功响应", "");
    }
}
